﻿using Avalon.Core.MVC.Actions;
using System;

namespace Avalon.Core.Providers {
    public class PipelineDescriptor : IPipelineDescriptor {
        public string Name { get; set; }
        public ResultType ResultType { get; set; }
        public Type PipelineType { get; set; }

        public IPipeline GetPipeline() {
            return GetPipeline(string.Empty);
        }

        public IPipeline GetPipeline(string entityName) {
            if (PipelineType.IsGenericType) {
                IEntityProvider entityProvider = ProviderContainer.Current.EntityProvider;
                IEntityDescriptor entityDescriptor = null;
                if (string.IsNullOrWhiteSpace(entityName)) {
                    entityDescriptor = entityProvider.GetEntityDescriptor();
                }
                else {
                    entityDescriptor = entityProvider.GetEntityDescriptor(entityName);
                }

                if (entityDescriptor != null) {
                    Type pType = PipelineType.MakeGenericType(entityDescriptor.EntityType);
                    return (IPipeline)Activator.CreateInstance(pType);
                }
            }
            else {
                return (IPipeline)Activator.CreateInstance(PipelineType);
            }

            return null;

        }
    }

    public enum ResultType {
        Collection,
        Single
    }
}
